package com.caisejingyu.whales.control.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.caisejingyu.whales.common.L;
import com.caisejingyu.whales.control.R;


public class FragmentDialogWrapper extends DialogFragment {
    public Fragment mFragment;
    private Builder mBuilder;
    private DialogCarterSucceedListener mDialogCarterSucceedListener;
    private int mPosition = Gravity.CENTER | Gravity.BOTTOM;

    public FragmentDialogWrapper() {
    }

    @SuppressLint("CommitTransaction")
    public void setBuilder(Builder builder) {
        mBuilder = builder;
        mFragment = builder.mFragment;
        if (mBuilder.mPosition != -1) {
            mPosition = mBuilder.mPosition;
        }
        show(builder.mFragmentManager, FragmentDialogWrapper.class.getName());
    }


    /**
     * 重写show()方法 处理dialog在后台显示的异常
     */
    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            FragmentTransaction ft = mBuilder.mFragmentManager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            L.e(FragmentDialogWrapper.class.getName(), e.toString());
        }
    }
//
//    public void show() {
//        show(mBuilder.mFragmentManager, FragmentDialogWrapper.class.getName());
//    }

    /**
     * 重写dismiss()方法 处理dialog在后台销毁的异常
     */
    @Override
    public void dismiss() {
        try {
            dismissAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.ControlDialogTheme);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.member_fragment_dialog_util, container);
        if (getWindow() != null) {
            Window window = getWindow();
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
            // 用此法，因为这个方法通知了DecorView更新LayoutParams
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
            // 设置位置
            window.setGravity(mPosition);
            getDialog().setCanceledOnTouchOutside(mBuilder.mCancel);
        }

        if (mBuilder.mBackgroundColor != 0) {
            view.findViewById(R.id.fragment_dialog_container).setBackgroundColor(mBuilder.mBackgroundColor);
        }

        getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                if (mDialogCarterSucceedListener != null) {
                    mDialogCarterSucceedListener.CarterSucceedCallback(getContext());
                }
            }
        });
        return view;
    }


    private Window getWindow() {
        return getDialog() == null ? null : getDialog().getWindow();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getChildFragmentManager().beginTransaction().replace(R.id.fragment_dialog_container, mFragment).commit();
    }

    /**
     * dialog创建成功的回调
     */
    public interface DialogCarterSucceedListener {
        void CarterSucceedCallback(Context context);
    }

    public void setmDialogCarterSucceedListener(DialogCarterSucceedListener mDialogCarterSucceedListener) {
        this.mDialogCarterSucceedListener = mDialogCarterSucceedListener;
    }

    public static class Builder {
        private FragmentManager mFragmentManager;
        private Fragment mFragment;
        private boolean mCancel;
        private int mBackgroundColor;
        private int mPosition = -1;

        public Builder(FragmentManager fragmentManager, Fragment fragment, boolean cancel) {
            this.mFragment = fragment;
            this.mFragmentManager = fragmentManager;
            this.mCancel = cancel;
        }

        public Builder(FragmentManager fragmentManager, Fragment fragment) {
            this.mFragment = fragment;
            this.mFragmentManager = fragmentManager;
        }

        public Builder setmBackgroundColor(int mBackgroundColor) {
            this.mBackgroundColor = mBackgroundColor;
            return this;
        }

        public Builder setPosition(int position) {
            this.mPosition = position;
            return this;
        }

        public FragmentDialogWrapper builder() {
            FragmentDialogWrapper dialogUtils = new FragmentDialogWrapper();
            dialogUtils.setBuilder(this);
            return dialogUtils;
        }
    }
}

